<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="用户">
                <select-user v-model="queryParam.userId" select-model="single" placeholder="请选择用户" />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="通知类型">
                <a-select placeholder="请选择通知类型" v-model="queryParam.type" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="通知标题">
                <a-input v-model="queryParam.title" placeholder="请输入通知标题" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="6" v-if="advanced">
              <a-form-item label="通知内容">
                <a-input v-model="queryParam.content" placeholder="请输入通知内容" allow-clear @keyup.enter.native="handleQuery" />
              </a-form-item>
            </a-col>
            <a-col :span="6" v-if="advanced" v-hasPermi="['platform:region:add']">
              <a-form-item label="地区">
                <select-region v-model="queryParam.regionId" placeholder="请选择地区" />
              </a-form-item>
            </a-col>
            <a-col :span="6" v-if="advanced">
              <a-form-item label="帖子">
                <select-post v-model="queryParam.itemId" placeholder="请选择帖子" />
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <notification-add-form v-if="showAddModal" ref="notificationAddForm" :typeOptions="typeOptions" @ok="getList" @close="showAddModal = false" />
      <!-- 编辑 -->
      <notification-edit-form v-if="showEditModal" ref="notificationEditForm" :typeOptions="typeOptions" @ok="getList" @close="showEditModal = false" />
      <advance-table
        title="推送表"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-notification-index-table"
        @change="handleTableChange"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="notificationList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd" v-hasPermi="['platform:notification:add']"> <a-icon type="plus" />新增 </a-button>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" v-hasPermi="['platform:notification:remove']"> <a-icon type="delete" />删除 </a-button>
          <a-button type="" @click="handleExport" v-hasPermi="['platform:notification:export']"> <a-icon type="download" />导出 </a-button>
        </div>
        <span slot="type" slot-scope="{ record }">
          <a-tag :color="typeColor(record.type)">{{ typeFormat(record) }}</a-tag>
        </span>
        <span slot="operation" slot-scope="{ record }">
          <a @click="handleUpdate(record)" v-hasPermi="['platform:notification:edit']"> 修改 </a>
          <a-divider type="vertical" v-hasPermi="['platform:notification:remove']" />
          <a @click="handleDelete(record)" v-hasPermi="['platform:notification:remove']"> 删除 </a>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { getInitData, listNotification, delNotification, exportNotification } from '@/api/platform/notification'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import NotificationAddForm from '@/views/platform/pushnotification/modules/NotificationAddForm'
import NotificationEditForm from '@/views/platform/pushnotification/modules/NotificationEditForm'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectRegion from '@/views/platform/components/SelectRegion'
import SelectPost from '@/views/platform/components/SelectPost'
export default {
  name: 'Notification',
  components: {
    AdvanceTable,
    NotificationAddForm,
    NotificationEditForm,
    SelectUser,
    SelectRegion,
    SelectPost
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 推送表表格数据
      notificationList: [],
      //推送类型
      typeOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        itemId: undefined,

        userId: undefined,

        regionId: undefined,

        title: undefined,

        content: undefined,

        type: undefined
      },
      columns: [
        {
          title: '帖子',
          dataIndex: 'postTitle',
          ellipsis: true,
          align: 'left',
          width: '18%'
        },
        {
          title: '用户',
          dataIndex: 'userName',
          ellipsis: true,
          align: 'left',
          width: '18%'
        },
        {
          title: '地区',
          dataIndex: 'regionName',
          ellipsis: true,
          align: 'left',
          width: '18%'
        },
        {
          title: '通知标题',
          dataIndex: 'title',
          ellipsis: true,
          align: 'left',
          width: '18%'
        },
        {
          title: '通知内容',
          dataIndex: 'content',
          ellipsis: true,
          align: 'left',
          width: '18%'
        },
        {
          title: '通知类型',
          dataIndex: 'type',
          align: 'center',
          width: '18%',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created() {
    getInitData('notification_type').then(response => {
      this.typeOptions = response.data.notification_type
    })
    this.getList()
  },
  methods: {
    /** 查询推送表列表 */
    getList() {
      this.loading = true
      const param = JSON.parse(JSON.stringify(this.queryParam))
      if (this.queryParam.userId) {
        param.userId = this.queryParam.userId.ids
      }
      listNotification(param).then(response => {
        this.notificationList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        itemId: undefined,

        userId: undefined,

        regionId: undefined,

        title: undefined,

        content: undefined,

        type: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleAdd() {
      this.showAddModal = true
      this.$nextTick(() => this.$refs.notificationAddForm.handleAdd())
    },
    handleUpdate(record, ids) {
      this.showEditModal = true
      this.$nextTick(() => this.$refs.notificationEditForm.handleUpdate(record, ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      const notificationIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk() {
          return delNotification(notificationIds).then(() => {
            that.onSelectChange([], [])
            that.getList()
            that.$message.success('删除成功', 3)
          })
        },
        onCancel() {}
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk() {
          return exportNotification(that.queryParam).then(response => {
            that.download(response.msg)
            that.$message.success('导出成功', 3)
          })
        },
        onCancel() {}
      })
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    // 类型徽标数类型
    typeColor(type) {
      if (type === '0') {
        return 'blue'
      } else if (type === '1') {
        return 'green'
      }
    }
  }
}
</script>
